<template>
    <view class="bottom-bar cart-bottom-bar">
        <view class="item-mian">
            <view class="price">
                <view>
                    <text>金额:</text>
                    <text class="price-text">{{ payPrice }}</text>
                    <text class="price-text small">元</text>
                </view>
            </view>
            <text class="btn btn-success" @click="pay">提交订单</text>
        </view>
        <uni-popup type="bottom" ref="payWay" background-color="#fff" :safe-area="false">
            <pay-way
                :payPrice="String(payPrice)"
                v-if="payList.length > 0"
                :payList="payList"
                @cancel="cancel"
                @select="select"></pay-way>
        </uni-popup>
    </view>
</template>

<script>
import { mapMutations, mapState, mapGetters } from "vuex"
import { submitOrderByUserCar, offlinePay } from "@/service/api/order.js"
import storage from "@/uni-sub-base/utils/storage.js"
import { getUserInfo } from "@/utils/appInit.js"
import { getUserCarList } from "@/utils/cartUtil.js"
import payWay from "@/components/common/pay-way.vue"
export default {
    components: {
        payWay,
    },
    props: {
        orderData: {
            type: Object,
            default: () => {},
        },
        useAddress: {
            type: Object,
            default: {},
        },
        ids: {
            type: String,
            default: "",
        },
        payList: {
            type: Array,
            default: [],
        },
        carPolicyList: {
            type: Array,
            default: () => [],
        },
        postPrice: {
            type: Number,
            default: 0,
        },
        isTempCar: {
            type: Boolean,
            default: false,
        },
        remark: {
            type: String,
            default: "",
        },
        payPrice: {
            type: Number,
            default: 0,
        },
        useCoupon: {
            type: Object,
            default: null,
        },
    },
    data() {
        return {
            total: null,
            balance: 0,
        }
    },
    watch: {
        payPrice(e) {
            this.$forceUpdate()
        },
    },
    mounted() {
        this.getInit()
    },
    methods: {
        ...mapMutations(["UP_CART_VIEW", "UP_ORDER_VIEW"]),
        //获取总额
        getTotal(postPrice) {
            //加上邮费
            if (this.total.price) {
                let total = Number(this.total.price)
                return total + postPrice
            }
            return 0
        },
        //初始化数据
        getInit() {},
        //点击支付
        pay() {
            if (this.checkAddress()) {
                return
            }
            this.$refs.payWay.open()
        },
        //查检收货地址
        checkAddress() {
            if (!this.useAddress || !this.useAddress.ID) {
                this.easyToast("请先选择收货地址")
                return true
            }
            return false
        },
        //提交订单
        submitOrder(payItem) {
            //检查收货地址
            if (this.checkAddress()) {
                return
            }
            //检查余额,余额是否够
            if (payItem.PaymentPayWay == 1 && (payItem.Balance <= 0 || payItem.Balance < this.payPrice)) {
                this.easyToast("您的余额不足，请前往充值再提交订单")
                return
            }
            //检查信用余额是否够
            if (payItem.PaymentPayWay == 10 && (payItem.Balance <= 0 || payItem.Balance < this.payPrice)) {
                this.easyToast("您的信用余额不足")
                return
            }
            this.$emit("action", payItem)
        },
        payApi(data, paramData) {
            let shop = storage.getCache("webInfo")
            let param = {
                businessId: shop.BusinessId,
                orderId: data.OrderId,
                opUserId: data.UserId,
                payWay: paramData.payWay,
                payType: 4,
                payAccountType: 0,
            }
            offlinePay(param).then((res) => {
                uni.hideLoading()
                this.upView()
                if (res.State == 1) {
                    //更新用户余额
                    getUserInfo().then((res) => {})
                    //跳转到支付结果
                    this.easyReLaunch("/pages_shop/packet/order/payResult?orderId=" + data.OrderId)
                } else {
                    this.easyToast(res.Msg)
                }
            })
        },
        cancel() {
            this.$refs.payWay.close()
        },
        select(payItem) {
            this.submitOrder(payItem)
            this.cancel()
        },
        upView() {
            //通知外面更新
            this.UP_CART_VIEW(new Date().getTime())
            this.UP_ORDER_VIEW(new Date().getTime())
        },
    },
}
</script>

<style lang="scss">
.cart-bottom-bar {
    flex-direction: row;
    z-index: 9;

    .item-mian {
        display: flex;
        flex-direction: row;
        align-items: center;
        flex: 1;
    }

    .price {
        display: flex;
        flex: 1;
        flex-direction: column;
        align-items: flex-end;
        justify-content: flex-end;
        margin-right: 20rpx;

        .price-text {
            font-size: $font-size-36;
            font-weight: 400;
            color: $color-red;
            margin-left: 20rpx;
        }
        .small {
            margin-left: 6rpx;
            font-size: $font-size-24;
            margin-bottom: 6rpx;
        }

        .num {
            text {
                margin-left: 10rpx;
            }
        }
    }
    .btn {
        border-radius: 100rpx;
    }
}
</style>
